package com.myaihui.service.impl;

import com.myaihui.domain.UserPropertyDossier;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.query.UserPropertyDossierQuery;
import com.myaihui.repository.UserPropertyDossierRepository;
import com.myaihui.service.UserPropertyDossierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.myaihui.foundation.constant.SysUserConstant.MARKET_ADMIN;
import static com.myaihui.foundation.constant.SysUserConstant.PM_ADMIN;

/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2018/6/11
 * *******************************
 */
@Service
public class UserPropertyDossierServiceImpl implements UserPropertyDossierService {
    @Autowired
    private UserPropertyDossierRepository userPropertyDossierRepository;

    @Override
    public Pager<UserPropertyDossier> findByConditions(UserPropertyDossierQuery dossierQuery) {
        Specification<UserPropertyDossier> specification = new Specification<UserPropertyDossier>() {
            @Override
            public Predicate toPredicate(Root<UserPropertyDossier> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(dossierQuery.getName()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("name"), "%" + dossierQuery.getName() + "%")));

                if (StringUtils.isNotBlank(dossierQuery.getPhone()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("phone"), "%" + dossierQuery.getPhone() + "%")));

                if (Objects.nonNull(dossierQuery.getStatus()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), dossierQuery.getStatus())));

                if (dossierQuery.getType() == PM_ADMIN)
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("propertyMgrId"), dossierQuery.getPlatformId())));

                if (dossierQuery.getType() == MARKET_ADMIN)
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("marketId"), dossierQuery.getPlatformId())));

                query.where(predicates.toArray(new Predicate[predicates.size()]));
                query.orderBy(criteriaBuilder.asc(root.get("status")), criteriaBuilder.desc(root.get("created")));
                return query.getRestriction();
            }
        };
        Pageable pageable = new PageRequest(dossierQuery.getiDisplayStart(), dossierQuery.getiDisplayLength());
        Page<UserPropertyDossier> page = userPropertyDossierRepository.findAll(specification, pageable);
        return new Pager(page, dossierQuery.getsEcho(), page.getContent());
    }

    @Override
    public UserPropertyDossier audit(String id, Integer status, String remark) {
        UserPropertyDossier dossier = userPropertyDossierRepository.findOne(id);
        dossier.setStatus(status);
        return userPropertyDossierRepository.save(dossier);
    }

    @Override
    public UserPropertyDossier detail(String id) {
        return userPropertyDossierRepository.findOne(id);
    }
}
